package org.example;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.example.controller.brandcontroller;
import org.example.domain.brand;
import org.example.domain.testcase;
import org.example.domain.user;
import org.example.service.brandservoce;
import org.example.service.userservice;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.web.servlet.*;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.ContentResultMatchers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.result.StatusResultMatchers;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@Transactional//添加事务
@Rollback(true)//true为回滚事务，不往数据库添加数据，false为不会滚，会往数据库添加数据
public class test2 {
    @Autowired
    private userservice userservice;
    @Autowired
private brandservoce brandservoce;
    @Autowired
private testcase testcase1;
    @Autowired
    JdbcTemplate jdbcTemplate;

//JdbcTemplate 技术，用于数据层持久化
    @Test
    public void jdbctemplateTest() throws Exception {
       List<brand> brands =(List) jdbcTemplate.queryForList("select * from tb_brand");
       System.out.println(brands);
    }
//    @Autowired
//    private MockMvc mvc;
@Test
    public void testcase(){
     System.out.println(testcase1);
    }
    @Test
    public void webtest1( @Autowired MockMvc mvc) throws Exception {
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/brand");
           mvc.perform(builder);
    }
    @Test
    public void webtest2( @Autowired MockMvc mvc) throws Exception {
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/brand");
        mvc.perform(builder);

        ResultActions action = mvc.perform(builder);

// 定义预期值：验证响应状态码为 200（OK）
        StatusResultMatchers status = MockMvcResultMatchers.status();
//        ContentResultMatchers status1 = MockMvcResultMatchers.content();
        ResultMatcher ok = status.isOk();

// 将预期结果与本次调用过程进行匹配
        action.andExpect(ok);
    }

    @Test
    public void test() {
        IPage users= userservice.getpage(5,10);
        Long s=userservice.getpage(5,10).getSize();
        Long s2=userservice.getpage(5,10).getPages();
        System.out.println(users);

        System.out.println("------>"+s);
        System.out.println("------>"+s2);

    }
    @Test
    public void test2() {
//        List<brand> brands= brandservoce.list();
//
//        System.out.println(brands);
 brand brand=new brand();
 brand brand1=new brand();
// brand.setBrandname("隆基绿能");
// brand.setCompanyname("隆基");
// brand.setOrdered(100);
// brand.setStatus(0);
// brand.setDescription("光伏龙头");
//      Object s=  brandservoce.save(brand);
//      System.out.println("------->"+s);
//        brand1.setId(20);
//        brand1.setBrandname("隆基绿能12");
//        brand1.setCompanyname("隆基12");
//        brand1.setOrdered(10012);
//        brand1.setStatus(0);
//        brand1.setDescription("光伏龙头12");
//        Object s2=  brandservoce.updateById(brand1);
//        System.out.println("......."+s2);
//        brandservoce.removeById(551751682);
        brandservoce.getById(1);

//       IPage page=  brandservoce.getPage(2,5);
//       System.out.println(page);
    }

}
